package com.usian.admin;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class AdChannelTestDemo {

    @Autowired
    private AdChannelMapper adChannelMapper;

    @Autowired
    MockMvc mvc; // 执行请求
    @Autowired
    ObjectMapper mapper;  //封装对象


    //使用mockMVC测试
    @Test
    public void savesFromMockMvc() throws Exception {
        AdChannel adChannel = new AdChannel();
        adChannel.setName("123456");
        // post get put delete
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/api/v1/AdChannel/save");
        builder.contentType(MediaType.APPLICATION_JSON_VALUE).content(mapper.writeValueAsBytes(adChannel));
        mvc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());


    }


    @Test
    public void findAll(){
        List<AdChannel> list = adChannelMapper.selectList(null);
        list.stream().forEach(x->{
            System.out.println(x.getName());
        });
    }

    @Test
    public void findBySearch(){
        // 适用于但条件
        //Wrappers.<AdChannel>lambdaQuery().like()

        //传统
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = "";

        lambdaQueryWrapper.like(name!=null&&!"".equals(name),AdChannel::getName,name);


        List<AdChannel> list =adChannelMapper.selectList(lambdaQueryWrapper);
    }

    @Test
    public void findBySearchPage(){
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = "";

        lambdaQueryWrapper.like(name!=null&&!"".equals(name),AdChannel::getName,name);

        Page<AdChannel> page = new Page<>(1,3);

        IPage<AdChannel> iPage =adChannelMapper.selectPage(page,lambdaQueryWrapper);
        System.out.println(iPage.getTotal());
    }

    // 添加
    @Test
    public void saves(){
        AdChannel adChannel = new AdChannel();
        adChannel.setName("测试");
        adChannelMapper.insert(adChannel);
    }

    @Test
    public void deletes(){
        List<Integer> list = new ArrayList<>();
        list.add(34);
        list.add(39);
        adChannelMapper.deleteBatchIds(list);
    }



}
